package com.zhihu_lvliangi_android.option.parent;

import java.io.Serializable;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zhihu_lvliangi_android.R;
import com.zhihu_lvliangi_android.service.MobileApplication;

/**
 * Fragment父类
 * 
 * @author azuryleaves
 * @since 2014-3-9 下午5:21:35
 * @version 1.0
 * 
 */
public abstract class ParentFragment extends BasicFragment implements Parcelable {

	/**
	 * The fragment argument representing the section number for this fragment.
	 */
	protected static final String ARG_SECTION_GROUP_NUMBER = "section_group_number";
	protected static final String ARG_SECTION_CHILD_NUMBER = "section_child_number";

	protected static String SaveFragmentKey = "ParentFragment";
	protected static LayoutInflater inflater;
	protected ParentActivity activity;
	protected Gson gson = new Gson();
	protected View parentView;
	private boolean useMain_scrollview = true; // -- 是否使用main_scrollview作为父视图
	private Toast toast;

	protected Bundle bundle;

	public interface ActionBarRightListener {
		public void onClick(View view, String className);
	}

	public interface PriceListener {

		public void refreshPrice(String totalPrice, String savePrice);

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		bundle = getArguments();
		if (bundle != null) {
			// if (params == null) {
			// Log.e(this.getClass().getSimpleName(), "传参Bundle对象包含的Object数组为空");
			// }
			// shop = (Shop) bundle.getSerializable(HomepageFragment.SHOP);
			// if (shop == null) {
			// Log.e(this.getClass().getSimpleName(), "传参Bundle对象包含的CloudItem为空");
			// }
		} else {
			Log.e(this.getClass().getSimpleName(), "传参Bundle对象为空");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ParentFragment.inflater = inflater;
		int layoutId = getLayoutId();
		parentView = inflater.inflate(R.layout.main_scrollview, container, false);
		if (layoutId == 0) {
			Log.e(MobileApplication.TAG, "请添加ContentView布局文件");
		} else {
			View childView = inflater.inflate(layoutId, container, false);
			if (childView instanceof ListView || childView instanceof GridView || childView instanceof ScrollView) {
				parentView = childView;
			} else {
				if (useMain_scrollview) {
					((ScrollView) parentView).addView(childView);
				} else {
					parentView = childView;
				}
			}
		}
	parentView.setBackgroundResource(R.color.white);
		setupViews(parentView);
		initialized(savedInstanceState);
		threadTask();
		if (useProgressFragment) {
			return super.onCreateView(inflater, container, savedInstanceState);
		} else {
			return parentView;
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (useProgressFragment) {
			// Setup content view
			setContentView(parentView);
			// Setup text for empty content
			setEmptyText(R.string.empty);
			setContentShown(false);
		}
	}

	protected void onSaveInstanceState(Bundle outState, ParentFragment fragment, Bundle bundle) {
		super.onSaveInstanceState(outState);
		outState.putAll(bundle);
		getActivity().getSupportFragmentManager().putFragment(outState, SaveFragmentKey, fragment);
	};

	protected ListView getGeneralListView() {
		ListView listView = (ListView) inflater.inflate(getGeneralListViewLayoutId(), null);
		return listView;
	}

	protected int getGeneralListViewLayoutId() {
		return R.layout.general_listview;
	}

	protected int getGeneralListViewInScrollViewLayoutId() {
		return R.layout.general_listview_in_scrollview;
	}

	/**
	 * 布局文件ID
	 * 
	 * @return
	 */
	protected abstract int getLayoutId();

	/**
	 * 初始化组件
	 */
	protected abstract void setupViews(View parentView);

	/**
	 * 初始化数据
	 */
	protected abstract void initialized(Bundle savedInstanceState);

	/**
	 * 需要线程池操作的任务
	 * 
	 */
	public abstract void threadTask();

	/**
	 * 恢复初始状态。例如：按钮恢复可点击状态
	 */
	public void recoveryView() {

	}

	protected void setArguments(ParentFragment fragment, Bundle bundle) {
		if (fragment != null) {
			if (bundle != null && !bundle.isEmpty()) {
				fragment.getArguments().putAll(bundle);
			} else {
				Log.i(this.getClass().getSimpleName(), "目标ParentFragment要传递的参数为空");
			}
		} else {
			Log.e(this.getClass().getSimpleName(), "目标ParentFragment为空");
		}
	}

	/**
	 * Fragment跳转，可返回
	 * 
	 * @param replaceLayout
	 * @param fragment
	 * @param fragmentName
	 * @param params
	 */
	protected void switchFragment(int replaceLayout, ParentFragment fragment, String fragmentName, Bundle bundle) {
		if (fragment != null) {
			if (bundle != null && !bundle.isEmpty()) {
				Bundle tempBundle = fragment.getArguments();
				if (tempBundle == null) {
					fragment.setArguments(bundle);
				} else {
					tempBundle.putAll(bundle);
				}
			} else {
				Log.i(this.getClass().getSimpleName(), "目标ParentFragment未传递参数");
			}
			getActivity().getSupportFragmentManager().beginTransaction().hide(this).add(replaceLayout, fragment, fragment.getClass().getSimpleName()).addToBackStack(fragmentName).commit();
		} else {
			Log.e(this.getClass().getSimpleName(), "目标ParentFragment为空");
		}
	}

	/**
	 * Fragment跳转，无返回
	 * 
	 * @param replaceLayout
	 * @param fragment
	 * @param params
	 */
	protected void switchFragmentNoBack(int replaceLayout, ParentFragment fragment, Bundle bundle) {
		if (fragment != null) {
			if (bundle != null && !bundle.isEmpty()) {
				fragment.getArguments().putAll(bundle);
			} else {
				Log.i(this.getClass().getSimpleName(), "目标ParentFragment未传递参数");
			}
			FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction();
			t.replace(replaceLayout, fragment);
			t.commit();
		} else {
			Log.e(this.getClass().getSimpleName(), "目标ParentFragment为空");
		}
	}

	/**
	 * 清除Fragment返回栈
	 * 
	 * @param fragmentActivity
	 */
	public static void popFragmentBackStack(FragmentActivity fragmentActivity) {
		int count = fragmentActivity.getSupportFragmentManager().getBackStackEntryCount();
		for (int j = 0; j < count; j++) {
			int backStackId = fragmentActivity.getSupportFragmentManager().getBackStackEntryAt(j).getId();
			fragmentActivity.getSupportFragmentManager().popBackStack(backStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);
		}
	}

	public void removeFragment() {
		getFragmentManager().beginTransaction().remove(this).commit();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.activity = (ParentActivity) activity;
		
	}

	/**
	 * 通过Class跳转界面
	 **/
	public void startActivity(Class<?> cls) {
		startActivity(cls, null);
	}

	/**
	 * 含有Bundle通过Class跳转界面
	 **/
	public void startActivity(Class<?> cls, Bundle bundle) {
		// if (isAdded()) {
		Intent intent = new Intent(activity, cls);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivity(intent);
		activity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
		// }
	}

	/**
	 * 通过Action跳转界面
	 **/
	public void startActivity(String action) {
		startActivity(action, null);
	}

	/**
	 * 含有Bundle通过Action跳转界面
	 **/
	public void startActivity(String action, Bundle bundle) {
		Intent intent = new Intent();
		intent.setAction(action);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivity(intent);
		activity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
	}

	/**
	 * 含有Bundle通过Class打开编辑界面
	 **/
	public void startActivityForResult(Class<?> cls, int requestCode) {
		startActivityForResult(cls, null, requestCode);
	}

	/**
	 * 含有Bundle通过Class打开编辑界面
	 **/
	public void startActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
		Intent intent = new Intent();
		intent.setClass(activity, cls);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivityForResult(intent, requestCode);
		activity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
	}

	/**
	 * 长时间显示Toast提示(来自String)
	 * 
	 * @param message
	 */
	protected void showToast(String message) {
		if (toast == null) {
			toast = Toast.makeText(activity, message, Toast.LENGTH_LONG);
		}
		toast.setDuration(Toast.LENGTH_LONG);
		toast.setText(message);
		toast.show();
	}

	/**
	 * 长时间显示Toast提示(来自res)
	 * 
	 * @param resId
	 */
	protected void showToast(int resId) {
		showToast(getString(resId));
	}

	/**
	 * 短暂显示Toast提示(来自res)
	 * 
	 * @param resId
	 */
	protected void showShortToast(int resId) {
		showShortToast(getString(resId));
	}

	/**
	 * 短暂显示Toast提示(来自String)
	 * 
	 * @param text
	 *            .
	 */
	protected void showShortToast(String message) {
		if (toast == null) {
			toast = Toast.makeText(activity, message, Toast.LENGTH_SHORT);
		}
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.setText(message);
		toast.show();
	}

	
	public boolean isUseMain_scrollview() {
		return useMain_scrollview;
	}
	/**
	 * oncreate 调用父类方法之前,否则可能出现问题
	 * @return
	 */
	public void setUseMain_scrollview(boolean useMain_scrollview) {
		this.useMain_scrollview = useMain_scrollview;
	}

	
	@Override
	public void onResponse(Object response, Enum<?> reqId) {
		// TODO Auto-generated method stub
		super.onResponse(response, reqId);
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		
	}
	
}
